package com.ckx.controller;

import com.alibaba.nacos.common.model.RestResult;
import com.ckx.cmd.HttpClientCmd;
import com.ckx.dao.entity.TbConfigDO;
import com.ckx.feign.BaiduApi;
import com.ckx.feign.QQApi;
import com.ckx.feign.TestApi;
import com.ckx.service.IConfigService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/test")
// @RefreshScope
public class TestController {

    @Resource
    private HttpClientCmd httpClientCmd;

    @Resource
    private BaiduApi baiduApi;

    @Resource
    private QQApi qqApi;

    @Resource
    private IConfigService configService;

    @Resource
    private TestApi testApi;

    @Value("${press.url}")
    private String pressUrl;

    @GetMapping
    public RestResult<String> init() {
        return RestResult.<String>builder().withData(pressUrl).build();
    }

    @GetMapping("run")
    public RestResult<String> run() throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        httpClientCmd.run();
        stopWatch.stop();
        return RestResult.<String>builder().withData("cost: " + stopWatch.getTotalTimeSeconds()).build();
    }

    @GetMapping("baidu")
    public RestResult<String> baidu() {
        return RestResult.<String>builder().withData(baiduApi.get()).build();
    }

    @GetMapping("qq")
    public RestResult<String> qq() {
        log.info("start req");
        return RestResult.<String>builder().withData(qqApi.get()).build();
    }


    @GetMapping("config/list")
    public RestResult<List<TbConfigDO>> configList(TbConfigDO configDO) {
        return RestResult.<List<TbConfigDO>>builder()
                .withData(configService.selectList(configDO))
                .build();
    }

    @GetMapping("api")
    public RestResult<String> api() {
        log.info("api test");
        return RestResult.<String>builder()
                .withData(testApi.get())
                .build();
    }

}
